﻿function isLinkHttp(url) {

    var isLinkHttp = url.search("http://");
    if (isLinkHttp < 0) {
        url.search("https://")
    }

    if (isLinkHttp < 0) {
        return false;
    } else {
        return true;
    }
}

function LogOut() {
    var btnlogout = document.getElementById('btnSiteHeaderLogout');
    if (btnlogout) { btnlogout.click(); }
}

// Genera un ID unico para cada TAB //
function tabHashCode(str, repeatable) {
    var hash = 1315423911;
    var randomnumber = Math.floor(Math.random() * 111) * repeatable;
    hash = hash + randomnumber;

    for (var i = 0; i < str.length; i++) {
        hash ^= ((hash << 5) + str.charCodeAt(i) + (hash >> 2));
    }

    return (hash & 0x7FFFFFFF);
}

function addTab(tabPanel, titlename, url, repeatable, applicationid, msgloading) {

    var id = "tab" + tabHashCode(titlename, repeatable);


    var tp = Ext.getCmp(tabPanel);
    var tab = tp.getItem(id);

    var c = ''; c = (url.indexOf("?")) > 0 ? "&" : "?";

    if (!tab) {
        //        var urlrequest = url.toString() + c + "applicationID=" + applicationid.toString() + "&tabID=" + id.toString();
        var urlrequest = url.toString();
        tab = tp.addTab({
            id: id.toString(),
            title: titlename,
            menuItem: item,
            closable: true,
            loader: {
                url: urlrequest,
                renderer: "frame",
                loadMask: {
                    showMask: true,
                    msg: msgloading
                }
            }

        });
        tp.setActiveTab(tab);

    } else {
        tp.setActiveTab(tab);
        tab.getTargetEl().frame()
    }

}

MenuPanel = {

    addTab: function (tabPanel, item, applicationid, msgloading) {
        if (Ext.isEmpty(item.url, false)) {
            return;
        }

        var repeatable = (Ext.isEmpty(item.repeatable, false) ? 0 : (item.repeatable == "1" ? 1 : 0));
        var passparentsize = (Ext.isEmpty(item.passParentSize, false) ? false : item.passParentSize);
        var id = "tab" + tabHashCode(item.text, repeatable);

        var tp = Ext.getCmp(tabPanel);
        var tab = tp.getComponent(id);


        if (isLinkHttp(item.url)) {
            window.open(item.url);
        } else {

            if (!tab) {
                var urlrequest = item.url;
                tab = tp.addTab({
                    id: id.toString(),
                    title: item.text,
                    menuItem: item,
                    closable: true,
                    loader: {
                        url: urlrequest,
                        renderer: "frame",
                        loadMask: {
                            showMask: true,
                            msg: msgloading
                        }
                    }
                });
                tp.setActiveTab(tab);
            } else {
                tp.setActiveTab(tab);
                //                Ext.getCmp(tab.tabEl).frame();
                tab.getTargetEl().frame()
            }

        }


    }

};